<?php
/**
 * 活跃度计算 (该脚本需要挂起在Linux后台服务中运行)
 *
 * 命令：
 * bash$ ./cli actives
 *
 * @author     楚吟风 <Chuyinfeng@gmail.com>
 */
class CLI_Actives extends CLI
{
    public function main()
    {
		//记录开始时间
		$time_start = microtime(TRUE);

		//加载活跃度相关类
		$actives=new Actives();

		$offset=0;
		$per_count=100;
		//循环获取所有可用店铺ID
		while (true) {	
			$rows = DB::select('shop_id')
                ->from('shops')             
                ->order_by('shop_id', 'ASC')     
                ->limit($per_count)
				->offset($offset)
				->fetch_all();

			//记录集遍历完毕
			if(count($rows)<1){
				//计算脚本运行耗时
				$time_script = round( microtime(TRUE) - $time_start , 3);
				CLI::output("Done at:" . date('Y-m-d H:i:s') ." \n script time:" . $time_script);
				die();
			}
		
			//根据记录集进行计算
			$j=0;
			foreach($rows as $item){
				$j++;	
				if($item['shop_id']){
					$score=$actives->calc($item['shop_id']);
					 DB::update('shops')->set(array('actives'=> $score))->where('shop_id','=',$item['shop_id'])->execute();
					CLI::output  ($offset + $j . "	 ,	" .  $item['shop_id'] . "	 ,	 "  . $score . "	\n");
				}
				
			}			

			$offset=$offset+$per_count;
		}


	
		 
      
    }
}